home *** CD-ROM | disk | FTP | other *** search
- /*
- ** File: Init.c
- **
- ** Written by: Bill Hayden
- ** Nikol Software
- **
- ** Copyright © 1995 Nikol Software
- ** All rights reserved.
- */
-
-
- #include "Init.h"
- #include "AEHandler.h"
- #include "Failure.h"
- #include <Palettes.h>
- #include "Menus.h"
- #include "MacWT.h"
-
- #include "Constants.h"
-
-
- static void StandardInitialization(short callsToMoreMasters);
- static void InitToolBox(void);
- static void StandardMenuSetup(short MBARID, short AppleMenuID);
- static void PullApplicationToFront(void);
-
-
- /*****************************************************************************/
-
-
-
-
- static void InitToolBox(void)
- {
- InitGraf(&qd.thePort);
- InitFonts();
- InitWindows();
- InitMenus();
- TEInit();
- InitDialogs(0L);
- InitCursor();
- }
-
-
-
- /*****************************************************************************/
-
-
-
- static void StandardInitialization(short callsToMoreMasters)
- {
- InitToolBox();
-
- while (callsToMoreMasters--) MoreMasters();
-
- PullApplicationToFront();
- }
-
-
-
- /*****************************************************************************/
-
-
-
-
- static void PullApplicationToFront(void)
- {
- #define kBroughtToFront 3
-
- EventRecord event;
- short count;
-
- for (count = 1; count <= kBroughtToFront; count++)
- EventAvail(everyEvent, &event);
- }
-
-
-
- /*****************************************************************************/
-
-
-
-
- void Initialize(short moreMasters, long minHeap, long minSpace)
- {
- long total, contig, qCPU;
- GDHandle theGDevice;
- OSErr err;
-
- StandardInitialization(moreMasters);
-
- /* We used to make a check for memory at this point by examining
- ** ApplLimit, ApplicZone, and StackSpace and comparing that to the minimum
- ** size we told the Finder we needed. This did not work well because it
- ** assumed too much about the relationship between what we asked
- ** the Finder for and what we would actually get back, as well as how to
- ** measure it. Instead, we will use an alternate method comprised of
- ** two steps. */
-
- /* It is better to first check the size of the application heap against a
- ** value that you have determined is the smallest heap the application can
- ** reasonably work in. This number should be derived by examining the
- ** size of the heap that is actually provided by the Finder when the
- ** minimum size requested is used. The derivation of the minimum size
- ** requested from MultiFinder is described in DTS.Lib.h. The check should
- ** be made because the preferred size can end up being set smaller than
- ** the minimum size by the user. This extra check acts to ensure that
- ** your application is starting from a solid memory foundation. */
-
- if ((long)GetApplLimit() - (long)ApplicationZone() < kMinMem)
- Fail(MemError(), __FILE__, __LINE__, TRUE);
-
- /* Next, make sure that enough memory is free for your application to run.
- ** It is possible for a situation to arise where the heap may have been of
- ** required size, but a large scrap was loaded which left too little
- ** memory. To check for this, call PurgeSpace and compare the result with
- ** a value that you have determined is the minimum amount of free memory
- ** your application needs at initialization. This number can be derived
- ** several different ways. One way that is fairly straightforward is to
- ** run the application in the minimum size configuration as described
- ** previously. Call PurgeSpace at initialization and examine the value
- ** returned. However, you should make sure that this result is not being
- ** modified by the scrap’s presence. You can do that by calling ZeroScrap
- ** before calling PurgeSpace. Make sure to remove that call before
- ** shipping, though. */
-
- PurgeSpace(&total, &contig);
- if (total < minSpace)
- Fail(MemError(), __FILE__, __LINE__, TRUE);
-
- /* The extra benefit to waiting until after the Toolbox Managers have been
- ** initialized to check memory is that we can now give the user an alert
- ** to tell him/her what happened. Although it is possible that the memory
- ** situation could be worsened by displaying an alert, MultiFinder would
- ** gracefully exit the application with an informative alert if memory
- ** became critical. Here we are acting more in a preventative manner to
- ** avoid future disaster from low-memory problems. */
-
- #if GENERATING68K
-
- // Make sure we have System 7.0 or newer, and a 68020 or newer (on 680x0's only)
- // On PowerPC's, both of these are a given, so we don't waste code checking them.
-
- Gestalt(gestaltSystemVersion, &total);
- Gestalt(gestaltProcessorType, &qCPU);
- total = (total >> 8) & 0xf;
- if ((total < 7) || (qCPU < 3))
- {
- StopAlert(rBadStartAlert, nil);
- ExitToShell();
- }
-
- #endif
-
- theGDevice = GetMainDevice();
-
- // We need at least an 8-bit monitor
-
- if ( !HasDepth( theGDevice, 8, 0, 0 ) )
- {
- StopAlert(rBadStartAlert, nil);
- ExitToShell();
- }
-
- // Save the current screen depth so we can restore it upon exit
-
- gScreenDepth = (*(*theGDevice)->gdPMap)->pixelSize;
-
- if ( gScreenDepth < 8 ) // Set depth to at least 8-bit, but…
- SetDepth( theGDevice, 8, 0, 0 );
-
- if ( gScreenDepth > 16 ) // …not to more than 16-bit.
- {
- err = SetDepth( theGDevice, 16, 0, 0 );
-
- if (err)
- {
- /*
- Some video cards, such as my Radius 24STV, support 24/32-bit but not 16-bit.
- In this case, a paramErr is returned and we must go to 8-bit, which we know
- will work because the above call to HasDepth(8) did not return an error.
- */
- SetDepth( theGDevice, 8, 0, 0 );
- }
- }
-
- // Save the depth we ended up at
-
- gCurrentDepth = (*(*theGDevice)->gdPMap)->pixelSize;
-
- if (gCurrentDepth == 16)
- {
- gTrueColor = TRUE;
- gSizeOfPixel = 2;
- }
-
- StandardMenuSetup(rMenuBar, mApple);
-
- qd.randSeed = TickCount();
- }
-
-
-
- /*****************************************************************************/
-
-
-
-
- void StandardMenuSetup(short MBARID, short AppleMenuID)
- {
- Handle menuBar = GetNewMBar(MBARID); /* Read menus into menu bar. */
- MenuHandle menu;
-
-
- if (!menuBar)
- Fail(ResError(), __FILE__, __LINE__, TRUE);
-
- SetMenuBar(menuBar); /* Install menus. */
- DisposeHandle(menuBar);
- AppendResMenu(GetMenuHandle(AppleMenuID), 'DRVR'); /* Add DA names to Apple menu. */
-
- menu = GetMenuHandle(mFile);
- if (menu)
- SetItemMark(menu, kShowFPS, (gShowFPS) ? checkMark : noMark);
-
- DrawMenuBar();
- }
-
-
-